package com.ceba.test.base.utils;

import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;

import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.ceba.test.base.otherjar.httpclient.APIHttpClient;


public class QRUtils {
	static Logger logger = LoggerFactory.getLogger(QRUtils.class);
	
	/**
	 * { "id":, "filePath":,"qrPdfUploadItf":,"qrPdfQueryItf": }
	 * 返回
	 * {"result":true,"message":""}
	 * @param json
	 * @return
	 */
	public static String uploadQRPdf(String json) {
		try {
			JSONObject o = new JSONObject(json);
			String id = "";
			String filePath = "";
			String qrPdfUploadItf="";
			String qrPdfQueryItf = "";
			if (o.has("id")) {
				id = o.getString("id");
			}
			if (o.has("filePath")) {
				filePath = o.getString("filePath");
			}
			if (o.has("qrPdfUploadItf")) {
				qrPdfUploadItf = o.getString("qrPdfUploadItf");
			}
			if (o.has("qrPdfQueryItf")) {
				qrPdfQueryItf = o.getString("qrPdfQueryItf");
			}

			String fileName = filePath.substring(filePath.lastIndexOf("/"), filePath.length());
			fileName = URLEncoder.encode(fileName, "utf-8");
			//上传地址
	        APIHttpClient client = new APIHttpClient(qrPdfUploadItf);
	        String re = "";
	        Map<String,String> maps = new HashMap<String,String>();
	        maps.put("qrQueryUrl", URLEncoder.encode(qrPdfQueryItf,"utf-8"));

	        re = client.uploadFileImpl(qrPdfUploadItf,filePath,"xx.pdf",maps,"text");
			
			JSONObject jsonr = new JSONObject(re);
			if(jsonr.has("result") && jsonr.getBoolean("result")){
				logger.debug("upload"+filePath+"，id = " +id+"到"+qrPdfUploadItf+"成功");
			}else{
				logger.debug("upload"+filePath+"，id = " +id+"到"+qrPdfUploadItf+"失败");
			}
			System.out.println(re);
		} catch (Exception e) {
			logger.error(e.getMessage());
		}

		return "";
	}
	
	public static void main(String []args){
		JSONObject o = new JSONObject();
		o.put("id", "315c1291803c4558bc4306eea86b5125");
		o.put("filePath", "E://workspace/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps/ep/upfile/sign/fm/2016-10-20/735a7fcbc773495ea19bc72ac589f4c9.pdf");
		o.put("qrPdfUploadItf", "http://192.168.1.38:8080/ep/qRPdfAction?action=uploadQRPdf");
		
		o.put("qrPdfQueryItf", "http://192.168.1.38:8080/ep/qRPdfAction?action=queryQRPdf");
		QRUtils.uploadQRPdf(o.toString());
	}
}
